/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddNewMachine.java
 *
 * Created on Oct 2, 2014, 3:59:03 PM
 */
package com.JD.master.frame;

import com.JD.datePicker.DatePicker;
import com.JD.master.Hibernate.MachineMaster;
import java.awt.print.PrinterException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author admin
 */
public class AddNewMachine extends javax.swing.JFrame {

    /** Creates new form AddNewMachine */
    public AddNewMachine() {
        initComponents();
        Edit_CheackBox.setSelected(false);


        com.JD.datePicker.DatePicker datePicker = new DatePicker();
        List year_List = datePicker.getYear_List();
        List day_List = datePicker.getDay_List();


        for (Object object : year_List) {

            year_Combobox.addItem(object.toString());
        }

        for (Object object : day_List) {

            date_ComboBox.addItem(object.toString());

        }

        String month[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

        month_Combobox.addItem(month[Integer.parseInt(new java.text.SimpleDateFormat("MM").format(new Date())) - 1]);
        for (String string : month) {

            month_Combobox.addItem(string);
        }


        SessionFactory sessionFactory = com.JD.master.Hibernate.Master_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();

        //com.JD.master.Hibernate.MachineMaster
        
        Query query = session.createQuery("from com.JD.master.Hibernate.MachineMaster");

         javax.swing.table.DefaultTableModel model1 = (javax.swing.table.DefaultTableModel) machine_Table.getModel();

          
        List list = query.list();


        for (Object object : list) {

            com.JD.master.Hibernate.MachineMaster  machineMaster=(com.JD.master.Hibernate.MachineMaster)object;
               model1.insertRow(0, new Object[]{machineMaster.getMachineName(), machineMaster.getMachineMake(), machineMaster.getMachineModel(), machineMaster.getMachineType()});
            

        }

    }
    com.JD.master.Validation.Validator validator = new com.JD.master.Validation.Validator();
    String machine_Name = "NO";
    String machine_make = "NO";
    String machine_Model = "NO";
    String machine_Type = "NO";
    String rawField1 = "STOP";
    String rawField2 = "NO";
    String rawField3 = "NO";
    String rawField4 = "0";

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        AddNewMachine_Panel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        machine_Table = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        machineType_ComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        machineModel_TextField = new javax.swing.JTextField();
        machineMake_TextField = new javax.swing.JTextField();
        machineName_TextField = new javax.swing.JTextField();
        AddDataToDatabase_Button = new javax.swing.JButton();
        year_Combobox = new javax.swing.JComboBox();
        month_Combobox = new javax.swing.JComboBox();
        date_ComboBox = new javax.swing.JComboBox();
        Edit_CheackBox = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        AddNewMachine_Panel.setBackground(new java.awt.Color(255, 102, 51));
        AddNewMachine_Panel.setMinimumSize(new java.awt.Dimension(100, 100));
        AddNewMachine_Panel.setPreferredSize(new java.awt.Dimension(1135, 627));

        machine_Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Machine Name", "Machine Make", "Machine Model", "Machine Type"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(machine_Table);

        jLabel1.setFont(new java.awt.Font("Traditional Arabic", 1, 24));
        jLabel1.setForeground(new java.awt.Color(18, 68, 200));
        jLabel1.setText("ADD NEW MACHINE");

        jLabel2.setFont(new java.awt.Font("Traditional Arabic", 1, 18));
        jLabel2.setForeground(new java.awt.Color(18, 68, 200));
        jLabel2.setText("Machine Make:");

        jLabel3.setFont(new java.awt.Font("Traditional Arabic", 1, 18));
        jLabel3.setForeground(new java.awt.Color(18, 68, 200));
        jLabel3.setText("Machine Model:");

        jLabel5.setFont(new java.awt.Font("Traditional Arabic", 1, 18));
        jLabel5.setForeground(new java.awt.Color(18, 68, 200));
        jLabel5.setText("Machine Name:");

        machineType_ComboBox.setEditable(true);
        machineType_ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CRUSHER", "D.G", "VEHICLE" }));

        jLabel6.setFont(new java.awt.Font("Traditional Arabic", 1, 18));
        jLabel6.setForeground(new java.awt.Color(18, 68, 200));
        jLabel6.setText("Machine Type:");

        machineMake_TextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                machineMake_TextFieldKeyReleased(evt);
            }
        });

        machineName_TextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                machineName_TextFieldKeyReleased(evt);
            }
        });

        AddDataToDatabase_Button.setText("Add Data");
        AddDataToDatabase_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddDataToDatabase_ButtonActionPerformed(evt);
            }
        });

        year_Combobox.setEditable(true);

        month_Combobox.setEditable(true);

        date_ComboBox.setEditable(true);

        Edit_CheackBox.setText("Edit");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/JD/PlaceOrder/excel.png"))); // NOI18N
        jButton1.setText("Export To Excel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/JD/PlaceOrder/print.png"))); // NOI18N
        jButton3.setText("Print");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout AddNewMachine_PanelLayout = new javax.swing.GroupLayout(AddNewMachine_Panel);
        AddNewMachine_Panel.setLayout(AddNewMachine_PanelLayout);
        AddNewMachine_PanelLayout.setHorizontalGroup(
            AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AddNewMachine_PanelLayout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(Edit_CheackBox)
                .addGap(108, 108, 108)
                .addGroup(AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(AddNewMachine_PanelLayout.createSequentialGroup()
                        .addGroup(AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                                .addComponent(jLabel5))
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(37, 37, 37)
                        .addGroup(AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(machineType_ComboBox, 0, 364, Short.MAX_VALUE)
                            .addComponent(machineMake_TextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
                            .addComponent(machineName_TextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
                            .addComponent(machineModel_TextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, AddNewMachine_PanelLayout.createSequentialGroup()
                                .addComponent(year_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(month_Combobox, 0, 140, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(date_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, AddNewMachine_PanelLayout.createSequentialGroup()
                                .addComponent(AddDataToDatabase_Button, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                                .addComponent(jButton1))))
                    .addGroup(AddNewMachine_PanelLayout.createSequentialGroup()
                        .addGap(236, 236, 236)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton3)
                .addGap(307, 307, 307))
            .addGroup(AddNewMachine_PanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1063, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(82, Short.MAX_VALUE))
        );
        AddNewMachine_PanelLayout.setVerticalGroup(
            AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AddNewMachine_PanelLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(AddNewMachine_PanelLayout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel1)
                        .addGap(45, 45, 45)
                        .addGroup(AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(machineName_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addGroup(AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(machineMake_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(29, 29, 29)
                        .addGroup(AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(machineModel_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addGroup(AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(machineType_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addGap(34, 34, 34)
                        .addGroup(AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(year_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(date_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(month_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AddNewMachine_PanelLayout.createSequentialGroup()
                        .addGap(257, 257, 257)
                        .addComponent(Edit_CheackBox)
                        .addGap(177, 177, 177)
                        .addGroup(AddNewMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(AddDataToDatabase_Button, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(11, 11, 11)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(AddNewMachine_Panel, javax.swing.GroupLayout.DEFAULT_SIZE, 1145, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(AddNewMachine_Panel, javax.swing.GroupLayout.DEFAULT_SIZE, 713, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void machineName_TextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_machineName_TextFieldKeyReleased
        // TODO add your handling code here:

        machineName_TextField.setText(machineName_TextField.getText().toUpperCase());


    }//GEN-LAST:event_machineName_TextFieldKeyReleased

    private void machineMake_TextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_machineMake_TextFieldKeyReleased
        // TODO add your handling code here:

        machineMake_TextField.setText(validator.stringValidator(machineMake_TextField.getText()).toUpperCase());

    }//GEN-LAST:event_machineMake_TextFieldKeyReleased

    private void AddDataToDatabase_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddDataToDatabase_ButtonActionPerformed
        // TODO add your handling code here:

        //TRACTOR

        if (Edit_CheackBox.isSelected()) {

            machine_Name = machineName_TextField.getText();
            machine_make = machineMake_TextField.getText();
            machine_Model = machineModel_TextField.getText();
            machine_Type = machineType_ComboBox.getSelectedItem().toString();

            SessionFactory sessionFactory = com.JD.master.Hibernate.Master_HibernateUtil.getSessionFactory();
            Session session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();

            Criteria cr = session.createCriteria(com.JD.master.Hibernate.MachineMaster.class);
            cr.add(Restrictions.eq("machineName", machine_Name));
            List results = cr.list();

            for (Object object : results) {

                com.JD.master.Hibernate.MachineMaster machineMaster = (com.JD.master.Hibernate.MachineMaster) object;

                machineMaster.setMachineName(machine_Name);
                machineMaster.setMachineMake(machine_make);
                machineMaster.setMachineModel(machine_Model);
                machineMaster.setMachineType(machine_Type);


                session.save(machineMaster);
                transaction.commit();


            }

            javax.swing.table.DefaultTableModel model1 = (javax.swing.table.DefaultTableModel) machine_Table.getModel();

            model1.insertRow(0, new Object[]{machine_Name, machine_make, machine_Model, machine_Type});

            machineName_TextField.setText("");
            machineMake_TextField.setText("");
            machineModel_TextField.setText("");
            machineType_ComboBox.removeAllItems();
            machineType_ComboBox.addItem("CRUSHER");
            machineType_ComboBox.addItem("D.G");
            machineType_ComboBox.addItem("VEHICLE");
            Edit_CheackBox.setSelected(false);

        } else {

            machine_Name = machineName_TextField.getText();
            machine_make = machineMake_TextField.getText();
            machine_Model = machineModel_TextField.getText();
            machine_Type = machineType_ComboBox.getSelectedItem().toString();

            SessionFactory sessionFactory = com.JD.master.Hibernate.Master_HibernateUtil.getSessionFactory();
            Session session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();


            Criteria cr = session.createCriteria(com.JD.master.Hibernate.MachineMaster.class);
            cr.add(Restrictions.eq("machineName", machine_Name));
            List results = cr.list();

            if (results.isEmpty()) {


                com.JD.master.Hibernate.MachineMaster machineMaster = new MachineMaster(machine_Name, machine_make, machine_Model, machine_Type, rawField1, rawField2, rawField3, rawField4);
                session.save(machineMaster);

                transaction.commit();

                javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) machine_Table.getModel();

                model.insertRow(0, new Object[]{machine_Name, machine_make, machine_Model, machine_Type});

                machineName_TextField.setText("");
                machineMake_TextField.setText("");
                machineModel_TextField.setText("");
                machineType_ComboBox.removeAllItems();
                machineType_ComboBox.addItem("CRUSHER");
                machineType_ComboBox.addItem("D.G");
                machineType_ComboBox.addItem("VEHICLE");

                Edit_CheackBox.setSelected(false);

            } else {
                JOptionPane.showMessageDialog(null, "Machine Name Is Already Exist");
            }






        }





    }//GEN-LAST:event_AddDataToDatabase_ButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:


        new com.JD.ExportToExcel.ExportToExcel().saveToExcel(machine_Table);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        try {
            // TODO add your handling code here:
            machine_Table.print();
        } catch (PrinterException ex) {
           JOptionPane.showMessageDialog(null, "Please Cheack Your Printer..");

        }
    }//GEN-LAST:event_jButton3ActionPerformed

    public JPanel getAddNewMachine_Panel() {
        return AddNewMachine_Panel;
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddDataToDatabase_Button;
    public javax.swing.JPanel AddNewMachine_Panel;
    private javax.swing.JCheckBox Edit_CheackBox;
    private javax.swing.JComboBox date_ComboBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField machineMake_TextField;
    private javax.swing.JTextField machineModel_TextField;
    private javax.swing.JTextField machineName_TextField;
    private javax.swing.JComboBox machineType_ComboBox;
    private javax.swing.JTable machine_Table;
    private javax.swing.JComboBox month_Combobox;
    private javax.swing.JComboBox year_Combobox;
    // End of variables declaration//GEN-END:variables
}
